//
//  SsehAddInvoiceTitleViewController.m
//  customer
//
//  Created by 杨阳 on 2018/11/1.
//  Copyright © 2018 yinlong. All rights reserved.
//

#import "SsehAddInvoiceTitleViewController.h"
#import "SsehAddInvoiceTitleTableViewCell.h"
#import "SsehConfigModel.h"

@interface SsehAddInvoiceTitleViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * ssehTableView;

@property (nonatomic,strong) NSMutableArray * dataSource , * rightDataSource;

@property (nonatomic,strong) UISegmentedControl * orderSgc;

@end

static NSString * const SsehAddInvoiceTitleTableViewCellID = @"SsehAddInvoiceTitleTableViewCellID";

@implementation SsehAddInvoiceTitleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseSetting];
    [self layoutUI];
}

- (void)baseSetting{
    
    self.title = @"添加抬头";
    
}

- (void)layoutUI{
    
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, 60)];
    headerView.backgroundColor = [UIColor backGroudColorForView];
    [self.view addSubview:headerView];
    
    self.orderSgc = [[UISegmentedControl alloc] initWithItems:@[@"企业", @"个人"]];
    self.orderSgc.tintColor = RGBColor(51, 51, 51);
    self.orderSgc.backgroundColor = [UIColor whiteColor];
    self.orderSgc.layer.borderColor = RGBColor(51, 51, 51).CGColor;
    self.orderSgc.layer.borderWidth = 1;
    self.orderSgc.clipsToBounds = YES;
    [self.orderSgc setSelectedSegmentIndex:0];
    CGFloat fontFloat = 14;
    
    [self.orderSgc setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontFloat], NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [self.orderSgc setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontFloat], NSForegroundColorAttributeName:RGBColor(51, 51, 51)} forState:UIControlStateNormal];
    
    [self.orderSgc addTarget:self action:@selector(orderSgcChangeValue:) forControlEvents:UIControlEventValueChanged];
    self.orderSgc.layer.cornerRadius = 7;
    [headerView addSubview:self.orderSgc];
    [self.orderSgc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView);
        make.centerX.equalTo(headerView);
        make.width.mas_equalTo(F_DEVICE_W-30);
    }];
    
    self.ssehTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 60, F_DEVICE_W, F_DEVICE_H-NAVIGATION_BAR_HEIGHT-bottomPadding(60)) style:UITableViewStyleGrouped];
    self.ssehTableView.delegate = self;
    self.ssehTableView.dataSource = self;
    self.ssehTableView.bounces = NO;
    self.ssehTableView.backgroundColor = [UIColor backGroudColorForView];
    self.ssehTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.ssehTableView.estimatedRowHeight = 0;
    self.ssehTableView.estimatedSectionHeaderHeight = 0;
    self.ssehTableView.estimatedSectionFooterHeight = 0;
    self.ssehTableView.showsVerticalScrollIndicator = NO;
    self.ssehTableView.showsHorizontalScrollIndicator = NO;
    [self.ssehTableView registerNib:[UINib nibWithNibName:@"SsehAddInvoiceTitleTableViewCell" bundle:nil] forCellReuseIdentifier:SsehAddInvoiceTitleTableViewCellID];
    [self.view addSubview:self.ssehTableView];
    
}

-(void)sureButtonClick{
    
    if (self.orderSgc.selectedSegmentIndex == 0) {
        
        for (NSInteger i = 0; i < self.dataSource.count; i++) {
            SsehConfigModel * model = self.dataSource[i];
            if (k_Is_Empty(model.infoStr)) {
                [self showTipWithTitle:model.infoStr1];
                return;
            }
        }
        
    }else{
        
        for (NSInteger i = 0; i < self.rightDataSource.count; i++) {
            SsehConfigModel * model = self.rightDataSource[i];
            if (k_Is_Empty(model.infoStr)) {
                [self showTipWithTitle:model.placeHodleStr];
                return;
            }
        }
    }
    
    if (self.orderSgc.selectedSegmentIndex == 0) {
        SsehConfigModel * leftModel0 = [[SsehConfigModel alloc]init];
        SsehConfigModel * leftModel1 = [[SsehConfigModel alloc]init];
        SsehConfigModel * leftModel2 = [[SsehConfigModel alloc]init];
        SsehConfigModel * leftModel3 = [[SsehConfigModel alloc]init];
        SsehConfigModel * leftModel4 = [[SsehConfigModel alloc]init];
        SsehConfigModel * leftModel5 = [[SsehConfigModel alloc]init];
        for (NSInteger i = 0; i < self.dataSource.count; i++) {
            switch (i) {
                case 0:
                    leftModel0 = self.dataSource[i];
                    break;
                case 1:
                    leftModel1 = self.dataSource[i];
                    break;
                case 2:
                    leftModel2 = self.dataSource[i];
                    break;
                case 3:
                    leftModel3 = self.dataSource[i];
                    break;
                case 4:
                    leftModel4 = self.dataSource[i];
                    break;
                case 5:
                    leftModel5 = self.dataSource[i];
                    break;
                default:
                    break;
            }
        }
        
        NSDictionary * par = @{@"member_id":K_UserDefault_Read(CustomerUserID),
                               @"member_token":K_UserDefault_Read(CustomerUserToken),
                               @"invoice_type":@"2",//类型 1:个人 2:公司
                               @"invoice_name":@"",//抬头内容
                               @"invoice_no":[NSString emptyStr:leftModel1.infoStr],//税号
                               @"invoice_company_name":[NSString emptyStr:leftModel0.infoStr],
                               @"invoice_company_address":[NSString emptyStr:leftModel2.infoStr],
                               @"invoice_company_phone":[NSString emptyStr:leftModel3.infoStr],
                               @"invoice_bank_name":[NSString emptyStr:leftModel4.infoStr],
                               @"invoice_bank_no":[NSString emptyStr:leftModel5.infoStr],
                               @"invoice_class":@"0"
                               };
        [self addInvoiceTitle:par];
        
    }else{
        
        SsehConfigModel * rightModel0 = [[SsehConfigModel alloc]init];
        for (NSInteger i = 0; i < self.dataSource.count; i++) {
            switch (i) {
                case 0:
                    rightModel0 = self.rightDataSource[i];
                    break;
                default:
                    break;
            }
        }
        NSDictionary * par = @{@"member_id":K_UserDefault_Read(CustomerUserID),
                               @"member_token":K_UserDefault_Read(CustomerUserToken),
                               @"invoice_type":@"1",//类型 1:个人 2:公司
                               @"invoice_name":[NSString emptyStr:rightModel0.infoStr],//抬头内容
                               @"invoice_no":@"",//税号
                               @"invoice_company_name":@"",
                               @"invoice_company_address":@"",
                               @"invoice_company_phone":@"",
                               @"invoice_bank_name":@"",
                               @"invoice_bank_no":@"",
                               @"invoice_class":@"0"
                               };
        [self addInvoiceTitle:par];
    }
    
}

-(void)addInvoiceTitle:(NSDictionary *)par{
    [self showLoading];
    [[QYNetManager sharedManager]POST:kInsertInvoice parameters:par andApiTyp:CustomerApiTypMain SuccessBlock:^(id responseObject) {
        [self hideLoading];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"]isEqualToString:@"ok"]) {
            if (responseObject[@"data"]) {
                [self showTipWithTitle:@"添加成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    } Failure:^(NSError *error) {
        [self hideLoading];
        [self showTipWithTitle:CustomerPoolNetStr];
    }];
    
}

-(void)orderSgcChangeValue:(UISegmentedControl *)seg{
    
    [self.ssehTableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.orderSgc.selectedSegmentIndex == 0) {
        return self.dataSource.count;
    }
    return self.rightDataSource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SsehAddInvoiceTitleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SsehAddInvoiceTitleTableViewCellID];
    
    SsehConfigModel * model = [[SsehConfigModel alloc]init];
    if (self.orderSgc.selectedSegmentIndex == 0) {
        model = self.dataSource[indexPath.row];
    }else{
        if (indexPath.row > 1) {
            return cell;
        }
        model = self.rightDataSource[indexPath.row];
    }
    
    [cell configData:model andIndex:indexPath];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 200;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 200)];
    footerView.backgroundColor = [UIColor backGroudColorForView];
    UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:16];
    sureButton.layer.cornerRadius = 5;
    sureButton.layer.borderWidth = 1.0f;
    sureButton.layer.borderColor = RGBColor(51, 51, 51).CGColor;
    [sureButton setTitle:@"添加发票抬头" forState:UIControlStateNormal];
    [sureButton setBackgroundColor:RGBColor(51, 51, 51)];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footerView).offset(100);
        make.left.equalTo(footerView).offset(15);
        make.right.equalTo(footerView).offset(-15);
        make.height.mas_equalTo(50);
    }];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        _dataSource = [[NSMutableArray alloc]initWithCapacity:0];
        for (NSInteger i = 0; i < 6; i++) {
            SsehConfigModel * model = [[SsehConfigModel alloc]init];
            [self.dataSource addObject:model];
            switch (i) {
                case 0:
                    model.titleStr = @"企业名称";
                    model.placeHodleStr = @"请输入企业名称";
                    model.infoStr1 = @"请输入企业名称";
                    model.infoStr = @"";
                    model.hiddenStay = @"0";
                    break;
                case 1:
                    model.titleStr = @"企业税号";
                    model.placeHodleStr = @"请输入纳税人识别号或社会统一征信代码";
                    model.infoStr1 = @"请输入企业税号";
                    model.infoStr = @"";
                    model.hiddenStay = @"0";
                    break;
                case 2:
                    model.titleStr = @"注册地址";
                    model.placeHodleStr = @"请输入企业注册地址";
                    model.infoStr1 = @"请输入企业注册地址";
                    model.infoStr = @"";
                    model.hiddenStay = @"0";
                    break;
                case 3:
                    model.titleStr = @"电话号码";
                    model.placeHodleStr = @"请输入企业电话号码";
                    model.infoStr1 = @"请输入企业电话号码";
                    model.infoStr = @"";
                    model.hiddenStay = @"0";
                    break;
                case 4:
                    model.titleStr = @"开户银行";
                    model.placeHodleStr = @"请输入企业开户银行（精确到支行）";
                    model.infoStr1 = @"请输入企业开户银行";
                    model.infoStr = @"";
                    model.hiddenStay = @"0";
                    break;
                case 5:
                    model.titleStr = @"银行账号";
                    model.placeHodleStr = @"请输入企业银行账号";
                    model.infoStr1 = @"请输入企业银行账号";
                    model.infoStr = @"";
                    model.hiddenStay = @"0";
                    break;
                default:
                    break;
            }
        }
    }
    return _dataSource;
    
}

- (NSMutableArray *)rightDataSource{
    if (_rightDataSource == nil) {
        _rightDataSource = [[NSMutableArray alloc]initWithCapacity:0];
        SsehConfigModel * model = [[SsehConfigModel alloc]init];
        model.titleStr = @"名称";
        model.placeHodleStr = @"请输入抬头名称";
        model.infoStr = @"";
        model.hiddenStay = @"1";
        [_rightDataSource addObject:model];
    }
    return _rightDataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
